<template>
	<el-form ref="form" :model="form" label-width="80px" @submit.prevent="onSubmit" style="width:100%;min-width:300px;">
		<el-form-item label="订单编号">
			<el-input v-model="form.orderId" :disabled="true"></el-input>
		</el-form-item>
		<!--<el-form-item label="商家shopuuid">-->
			<!--<el-input v-model="form.shopuuid"></el-input>-->
		<!--</el-form-item>-->
		<el-form-item label="分销商" prop="useruuid">
			<el-select v-model="form.uid" placeholder="请选择分销商"
					   @change="userChange">
				<el-option value="0" label="全部">全部</el-option>
				<el-option
						v-for="item in users"
						:key="item.user.useruuid"
						:label="item.user.username"
						:value="item.user.useruuid">
				</el-option>
			</el-select>
		</el-form-item>

		<!--<el-form-item label="产品ID">-->
			<!--<el-input v-model="form.productuuid"></el-input>-->
		<!--</el-form-item>-->

		<el-form-item label="产品名称" prop="productuuid">
			<el-select v-model="form.pid" placeholder="请选择产品" @change="productChange">
				<el-option value="0" label="全部">全部</el-option>
				<el-option
						v-for="item in products"
						:key="item.downprouuid"
						:label="item.productName"
						:value="item.downprouuid">
				</el-option>
			</el-select>
		</el-form-item>
		<el-form-item label="充值金额">
			<el-input v-model="form.amount" :disabled="true"></el-input>
		</el-form-item>

		<el-form-item label="充值号码">
			<el-input  type="textarea" v-model="form.phone" :rows="15"
			placeholder="
			批量充值时格式为:号码+空格+面值
			18011112222 100
			18900002222 50
			15922223333 30"  ></el-input>
		</el-form-item>
		<el-form-item label="回调地址">
			<el-input v-model="form.notify" :disabled="true"></el-input>
		</el-form-item>
		<el-form-item label="时间戳">
			<el-input v-model="form.ts"></el-input>
		</el-form-item>
		<!--<el-form-item label="key">-->
			<!--<el-input v-model="form.key"></el-input>-->
		<!--</el-form-item>-->
		<el-form-item label="SIGN">
			<el-input v-model="form.sign"></el-input>
		</el-form-item>
		<el-form-item style="color: red">
			<span>单个提交，点击签名后在点击提交充值，批量提交按格式输入后直接点击批量充值进行提交</span>
			</br>
			<span>批量提交单次建议不要超过1000个</span>
		</el-form-item>
		<el-form-item>
			<!--<el-button type="primary" v-on:click="signMethod">签名</el-button>-->
			<!--<el-button type="primary" v-on:click="onSubmit">提交充值</el-button>-->
			<el-button type="primary" v-on:click="onrestBatch">批量充值</el-button>
		</el-form-item>
	</el-form>
</template>

<script>
	import {
		testProduct,
		findAllDownShopInfo,
		findAllDownProduct,
		findDownShopInfoKey,
		getConfig,
		restBatch
	} from '../../api/api';
    var crypto = require('crypto');


	export default {
		data() {
//     {
//			    "orderId ":"xxxxxxx",
//			    "uid":"xxxxxxxxxxxx",
//			    "sign":"3183496d7f0cb397b819bdc89a11c0fc",
//			    "pid":"4028b8815f7d4ae2015f86ee6aa70009",
//			    "phone":"18000001111",
//			    "notify":"http://xxx:port/call",
//			    "ts":"20170928115522"
//			}
			return {
				form: {
                    orderId: 'yidongpingtai'+new Date().getTime(),
                    uid: '0',
                    amount: '',
                    pid: '',
                    phone: '',
                    notify: '',
                    key: '',
                    sign:'',
                    ts:new Date().getTime()
				},
                users:[],
                products:[]
			}
		},
		methods: {
			onSubmit() {
				const phones = this.form.phone.split("\n");
				if(phones.length > 1){
					this.$message({
						message: "号码过多请选择批量提交",
						type: 'error'
					});
				}else{
					if(this.form.uid != "0" ){
						if( this.form.orderId != ""){
							if(this.form.pid != "0" && this.form.pid != ""){
								if(this.form.key != ''){
									let para = Object.assign({}, this.form);
									para.amount = para.amount+"";
									para.ts = para.ts+"";
									delete para.amount;
									delete para.key;
									delete para.shopuuid;
									testProduct(para).then( datas => {
										let { message, code, data } = datas;
										if (code !== "0") {
											this.$message({
												message: message,
												type: 'error'
											});
										}else{
											this.$message({
												message: "提交成功",
												type: 'info'
											});
										}
										this.form.orderId = 'yidongpingtai'+new Date().getTime();
										this.form.ts = new Date().getTime();
									});
								}else{
									this.$message({
										message: "请刷新后再尝试",
										type: 'error'
									});
								}
							}else{
								this.$message({
									message: "请选择充值测试产品",
									type: 'error'
								});
							}
						}else{
							this.$message({
								message: "订单编号不能为空",
								type: 'error'
							});
						}
					}else{
						this.$message({
							message: "请选择充值测试渠道",
							type: 'error'
						});
					}
				}
			},
			onrestBatch:function(){
				const phones = this.form.phone.split("\n");
				const kongge = phones.find(item =>{ return item == ""});
				console.log(kongge)
				if(!kongge && kongge != undefined){
					this.$message({
						message: "格式错误,请仔细检查,如空格",
						type: 'error'
					});
				}else{
					if(phones.length == 0 || phones[0] == ""){
						this.$message({
							message: "号码不能为空",
							type: 'error'
						});
					}else {
						if(this.form.uid == "0" ){
							this.$message({
								message: "请选择分销商",
								type: 'error'
							});
						}else{
							var orders = [];
							if(phones){
								for(var i = 0 ; i < phones.length; i++){
									var order = new Object();
									order.orderId = Math.random().toString(36).substr(2)+new Date().getTime();
									order.uid = this.form.uid;
									const ph = phones[i].indexOf(" ") != -1 ? phones[i].split(" ") : phones[i].split("	");
									if(!ph[0] || !ph[1]){
										console.log("异常号码:",phones[i])
										continue;
									}
									order.phone = ph[0];
									order.money = ph[1];
									order.notify = this.form.notify;
									order.ts = this.form.ts;
									orders.push(order)
								}
							}
							restBatch(JSON.stringify(orders)).then( datas => {
								let { message, code, data } = datas;
								if (code !== "0") {
									this.$message({
										message: message,
										type: 'error'
									});
								}else{
									this.$message({
										message: "提交成功",
										type: 'info'
									});
								}
								this.form.orderId = 'weiliupingtai'+new Date().getTime();
								this.form.ts = new Date().getTime();
							});
						}
					}
				}
			}
			,findAllDownShopInfo: function () {
                let para = {useruuid: JSON.parse(sessionStorage.getItem("user")).useruuid};
                findAllDownShopInfo(para).then(datas => {
                    let {message, code, data} = datas;
                    if (code !== "0") {
                        this.$message({
                            message: message,
                            type: 'error'
                        });
                    } else {
                        this.users = data;
                    }
                });
            },getConfig(){
                let para = {useruuid: JSON.parse(sessionStorage.getItem("user")).useruuid};
                getConfig(para).then(datas => {
                    let {message, code, data} = datas;
                    if (code !== "0") {
                        this.$message({
                            message: message,
                            type: 'error'
                        });
                    } else {
                        this.form.orderId = data.testorderprefix+new Date().getTime();
                        this.form.notify = data.testcallbackurl;
                    }
                });
			},
			userChange: function (shopuuid) {
				this.form.shopuuid = shopuuid;
                let para = {useruuid: this.form.uid};
                findAllDownProduct(para).then(datas => {
                    let {message, code, data} = datas;
                    if (code !== "0") {
                        this.$message({
                            message: message,
                            type: 'error'
                        });
                    } else {
                        this.products = data;
                    }
                });
                findDownShopInfoKey(para).then(datas => {
                    let {message, code, data} = datas;
                    if (code !== "0") {
                        this.$message({
                            message: message,
                            type: 'error'
                        });
                    } else {
                       this.form.key = data;
                    }
                });
			},productChange:function(productuuid){
				for(var i = 0 ; i < this.products.length ; i++){
				    if(this.products[i].downprouuid == productuuid){
                        this.form.amount = this.products[i].price;
					}
				}
			},
			signMethod(){
                var md5 = crypto.createHash('md5');
                var string = "notify="+this.form.notify+"&orderId="+this.form.orderId+"&phone="+this.form.phone+"&pid="+this.form.pid+"&ts="+this.form.ts+"&uid="+this.form.uid+"&key="+this.form.key;
				this.form.sign =  md5.update(string).digest('hex');
			}
		},mounted() {
            this.findAllDownShopInfo();
            this.getConfig();
        }
	}

</script>
